package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.service.TbManufactorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.entity.TbManufactor;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/manufactor")
public class TbManufactorController extends BaseController {
	
	@Autowired
	private TbManufactorService tbManufactorService;

	//配置--厂家管理   （点击查询）
	@RequestMapping(value="/findAllManufactor.do")
	public ModelAndView findAllManufactor(String searchTxt,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("manufactorName", searchTxt);
			map.put("manufactorBrand", searchTxt);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbManufactor> page = tbManufactorService.findAllManufactorByConPage(map);
		request.setAttribute("mfList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/peizhi/changjia/manufactor_list");
	}
	
	@RequestMapping(value="/manufactorEdit.do")
	public String manufactorEdit(Integer id,HttpServletRequest request,HttpServletResponse response)throws BusinessException{
		TbManufactor manufactor = (id == null) ? new TbManufactor() : tbManufactorService.selectByPrimaryKey(id);
		if(id == null){
			SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmss");
			String no =sdf.format(new Date())+(int)Math.random()*100;
			manufactor.setManufactorNo(no);
		}
		request.setAttribute("manufactor", manufactor);
		return "/peizhi/changjia/manufactor_edit";
	}
	
	@RequestMapping(value="/save.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<Object,Object> addOrUpdate(TbManufactor manufactor ,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			manufactor.setManufactorCreatetime(new Date());
			if(manufactor.getManufactorId() == null ){
				tbManufactorService.insert(manufactor);
			}else{
				tbManufactorService.updateByPrimaryKey(manufactor);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public Map<Object,Object> delete(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				tbManufactorService.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
}
